package com.wasu.service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wasu.bean.AdSchedule;

@Controller
@RequestMapping("/order")
public class QuerySchedule{
	private static Logger log = Logger.getLogger(QuerySchedule.class);

	@ResponseBody
	@RequestMapping("/queryschedule")
	protected List<AdSchedule> queryschedule(@RequestParam("adcode") String adcode) {
		log.info(adcode);
		List<AdSchedule> list = OrderJedis.queryAdSchedules(adcode);
		Collections.sort(list, new Comparator<Object>() {
			public int compare(Object a, Object b) {
				DateTimeFormatter df = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
				DateTime fromdate = df.parseDateTime(((AdSchedule) a).getExposuredate() + " 00:00:00");
				DateTime enddate = df.parseDateTime(((AdSchedule) b).getExposuredate() + " 00:00:00");
				Period period = new Period(fromdate, enddate);
				return period.getDays();
			}
		});
		return list;
	}
	
}
